$(function () {
  loadGridTable();
  $.get('cats.action',"",processCatsResponse);
});

function loadGridTable() {
  $("#gridTable").jqGrid({
    url: "items.action?approveStatus="+$("#approve_status").val(),
    datatype: "json",
    height: 450,
    width: 450,
    colNames: ["编号", "图片", "名称", "价格", "下架时间", "pic_url","frank_title"],
    colModel: [{
      name: "iid",
      index: "iid",
      hidden: true
    },
    {
      name: "pic_url",
      index: "pic_url",
      formatter:formatItemPic,
      width: "60",
      align:"center",
      sortable: false
    },
    {
      name: "title",
      index: "title",
      formatter:formateadorLink,
      width: "150"
    },
    {
      name: "price",
      index: "price",
      sorttype: "int",
      width: "80"
    },
    {
      name: "delist_time",
      index: "delist_time",
      hidden: true
    },
    {
      name: "pic_url",
      index: "pic_url",
      hidden: true
    },
    {
      name: "frank_title",
      index: "frank_title",
      hidden: true
    }
    ],
    sortname: "delist_time",
    sortorder: "desc",
    viewrecords: true,
    rowNum: 7,
    rowList: [7, 15, 25],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
    	setGridTableSelection();
        setGridCellHeight('gridTable',60);
    },
    onSelectRow: function (rowid, status) {
      if (rowid === null) {
        return;
      }
      var ret = $("#gridTable").jqGrid("getRowData", rowid);
      var oldsrc = $("#" + ret.iid + " img").attr("src");
      if (status) {
        if (oldsrc!==undefined && oldsrc!==null) {return;}
     
     	var title_html = buildSimpleTitle(ret);
        var onClickButtonHtml = "<input type='button' class='btn2 pbtn_60' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='删除' style=\"border-color: rgb(220, 220, 220);\" onclick=\"delete_item('" + ret.iid + "')\">";
        var result = "<div id='" + ret.iid +  "' class='bePromoted' price='" + ret.price + "' item_title='"+ret.frank_title+"' title='"+ret.frank_title+"'> <img src='" + ret.pic_url + "'/><p>" +onClickButtonHtml+ "</p></div>";
        $("#choosed_item_container").append(result);
      } else {
        $("#" + ret.iid).remove();
      }
    },
    onSelectAll: function (ids, status) {
      var action;
      if (ids === null || ids.length === 0) {
        return;
      }
      if (status) {
        for (var i = 0; i < ids.length; i=i+1) {
          var ret = $("#gridTable").jqGrid("getRowData", ids[i]);
          var oldsrc = $("#" + ret.iid + " img").attr("src");
          
          if (oldsrc===undefined || oldsrc === null) {
            var title_html = buildSimpleTitle(ret);
            var onClickButtonHtml = "<input type='button' class='btn2 pbtn_60' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='删除' style=\"border-color: rgb(220, 220, 220);\" onclick=\"delete_item('" + ret.iid + "')\">";
	        var result = "<div id='" + ret.iid +  "' class='bePromoted' price='" + ret.price + "' item_title='"+ret.frank_title+"'> <img src='" + ret.pic_url + "'/><p>" +onClickButtonHtml+ "</p></div>";
	        $("#choosed_item_container").append(result);
          }
        }
      } else {
        for (i = 0; i < ids.length; i=i+1) {
          ret = $("#gridTable").jqGrid("getRowData", ids[i]);
          $("#" + ret.iid).remove();
        }
      }
      //sub_itemchoose_action($("#iids_hidden").val());
    },
    jsonReader: {
      root: "dataset",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#gridPager",
    caption: "宝贝列表"
  });
}

function formatItemPic(cellvalue, options, rowObject){
  return "<img src='"+cellvalue+"_60x60.jpg'></img>";
}

function formateadorLink(cellvalue, options, rowObject) {
   var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>"+cellvalue+"</span>";
   return "<a href=http://item.taobao.com/item.htm?id="+ rowObject.iid + " target='_blank'>" + title + "</a>";
}

$(function() {
	$('#item_key').keypress(function(event) {
	    if ((event.keyCode == 13)) {// 在这里接收的是Enter事件,S的ASCII码为13。
	       doSearch();
	    }
  	});
    
  	$('#itm_search_btn').click(function(){
    	doSearch();
  	});
});


$(function () {
	$("#approve_status").change(function () {
    	doSearch();
  	});
  
  	$("#sellerCats").change(function () {
    	doSearch();
  	});
});

function doSearch() {
  var url = "items.action?";
  url += "approveStatus=" + $("#approve_status").val();
  url += "&searchKey="+encodeURI(encodeURI($("#item_key").val()));
  url += "&sellerCids="+$("#sellerCats").val();
  
  $("#gridTable").jqGrid("setGridParam", {
    url: url,
    page: 1
  }).trigger("reloadGrid");
}


function processCatsResponse(data, status) {
  var sellerCats = data.sellerCats;
  var sellerCatsSelect = "<option value='all_cats'>---所有宝贝---</option>";
  $.each(sellerCats, function (catref, cat) {
    var cat_name = cat.name;
    if(cat.isLeaf=='2'){
      cat_name = '└'+cat.name;
    }
    if(cat.isLeaf=='0'){
      sellerCatsSelect += "<optgroup label="+ cat_name + "></optgroup>";
    }else{
      sellerCatsSelect += "<option value='" + cat.cid + "'>"+ cat_name + "</option>";
    } 
  });
  sellerCatsSelect += "<option value ='-1'>未分类宝贝</option>";
  $("#sellerCats").html(sellerCatsSelect);
}

function validate_tab2() {
	if(empty(buildItemJsonStr())) {
		alert("请选择要推荐的宝贝!");
		return false;
	}
	return true;
}
